// pages/FirstPage/FirstPage.js
const api = require("../../config/api.js")
var amapFile = require('../../libs/amap-wx.130.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    psCustomerCode: "",
    sensorData: [],
    deviceData: [],
    firstIndexData: {},
  },

  onData_Detail_sensor: function (e) {
    const dataIndex = e.currentTarget.dataset.index; // 获取当前点击的报告的索引
    const selectedDevice = this.data.sensorData[dataIndex];
    console.log('selectedDevice')
    console.log(selectedDevice)
    wx.navigateTo({
      url: `/pages/Data-Detail/Data-Detail?sensorID=${selectedDevice.sensorID}`,
    })
  },
  onData_Detail_device: function (e) {
    const dataIndex = e.currentTarget.dataset.index; // 获取当前点击的报告的索引
    const selectedDevice = this.data.deviceData[dataIndex];
    console.log('selectedDevice')
    console.log(selectedDevice)
    wx.navigateTo({
      url: `/pages/Data-Detail/Data-Detail?deviceID=${selectedDevice.deviceID}`,
    })
  },

  onFix: function (e) {
    wx.navigateTo({
      url: '/pages/FirstPage/FixOrWarn/FixOrWarn?feedbackType=2',
    })
  },
  onWarn: function (e) {
    wx.navigateTo({
      url: '/pages/FirstPage/FixOrWarn/FixOrWarn?feedbackType=1',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  onShow(){
    this.resetData()
    this.fetchIndexData();
    // this.fetchSensorData();
    this.getLocation();
    this.fetchDeviceData();
  },
  getLocation() {
    // 获取用户位置
    var that = this;
    var myAmapFun = new amapFile.AMapWX({
      key: 'd7d8fdf44090282bb11954ab2acc852a'
    });
    myAmapFun.getRegeo({
      success: function (data) {
        //成功回调
        // console.log('位置：', data)
        that.setData({
          location:data[0].name
        })
      },
      fail: function (info) {
        //失败回调
        console.log(info)
      }
    })
  },
  fetchIndexData: function (params = {}) {
wx.getStorage({
      key: 'psCustomerCode',
      success: (res) => {
        this.setData({
          psCustomerCode: res.data
        });    
        wx.request({
          url: api.firstIndexData,
          method: 'POST',
          data: {
            customerCode: this.data.psCustomerCode,
          },
          success: (res) => {
            wx.hideLoading()
            if (res.data.code == 200) {
              const firstIndexData = res.data.data;
              this.setData({
                firstIndexData: firstIndexData
              });
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
  },
  fetchSensorData: function (params = {}) {
    wx.getStorage({
      key: 'psCustomerCode',
      success: (res) => {
        this.setData({
          psCustomerCode: res.data
        });
        console.log(this.psCustomerCode)
        wx.request({
          url: api.sensorData,
          method: 'POST',
          data: {
            customerCode: this.data.psCustomerCode,
            pageNum: "1",
            pageSize: "20",
          },
          success: (res) => {
            wx.hideLoading()
            if (res.data.code == 200) {
              this.setData({
                sensorData: res.data.data.rows
              });
              console.log(res.data.data.rows)
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
  },
  fetchDeviceData: function (params = {}) {
    wx.getStorage({
      key: 'psCustomerCode',
      success: (res) => {
        this.setData({
          psCustomerCode: res.data
        });
        wx.request({
          url: api.deviceData,
          method: 'POST',
          data: {
            customerCode: this.data.psCustomerCode,
            pageNum: this.data.pageNum,
            pageSize: "5",
          },
          success: (res) => {
            wx.hideLoading()
            if (res.data.code == 200) {
              const newDeviceData = res.data.data.rows
              console.log("设备数据：", res.data.data)
              if (newDeviceData.length!=0) {
                this.setData({
                  pageNum: this.data.pageNum + 1,
                });
              }
              this.setData({
                deviceData: [...this.data.deviceData, ...newDeviceData]
              });
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
  },
  onReachBottom() {
    console.log('刷新')
  },
  // 滑到底部
  onScrollToLower: function () {
    console.log('滑到末尾，刷新')
    this.fetchDeviceData()
  },
  // 重新获取数据
  onScrollToUpper: function () {
    console.log('刷新')
    this.resetData()
  },
  resetData: function () {
    this.setData({
      pageNum: 1,
      deviceData: [],
    })
    //查询
  }
})